//
//  AccessWebSiteView.m
//  BabyHelper
//
//  Created by  thomas on 11-8-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AccessWebSiteView.h"


@implementation AccessWebSiteView

@synthesize trafficResult,bGetServerResult,bRequestSent;
// Implement viewDidLoad to do additiWEATHER_onal setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];	
	[self SetBarItemAsButton];
	self.title = NSLocalizedString(@"iBabyNotes",@"");
	if(isNetworkAccessable())
	{
		[webView loadHTMLString:NSLocalizedString(@"iBabyNotes",@"") baseURL:[NSURL URLWithString:WEBSITE_URL]];	
		[self SendTrafficTextRequest];
		[self SetBarItemAsIndicator];
	}else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
														message:NSLocalizedString(@"No Network Connection", @"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}


- (void)viewWillAppear:(BOOL)animated {
	
	self.bRequestSent     = false;
	self.bGetServerResult = false;
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.trafficResult = nil;
}

-(void)toggleQuery{
	if(isNetworkAccessable())
	{
		[webView loadHTMLString:NSLocalizedString(@"iBabyNotes",@"") baseURL:[NSURL URLWithString:WEBSITE_URL]];	
		[self SendTrafficTextRequest];
		[self SetBarItemAsIndicator];
	}else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"") 
														message:NSLocalizedString(@"No Network Connection", @"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}

-(void)SetBarItemAsIndicator{
	UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0,0, 20, 20)];
    [activityIndicator startAnimating];
    UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    [activityIndicator release];
    self.navigationItem.rightBarButtonItem = activityItem;
    [activityItem release];
}

-(void)SetBarItemAsButton{
	UIBarButtonItem *queryItem = [[UIBarButtonItem alloc]
								  initWithTitle:NSLocalizedString(@"Refresh",@"")
								  style:UIBarButtonItemStyleBordered 
								  target:self
								  action:@selector(toggleQuery)		
								  ];
	self.navigationItem.rightBarButtonItem = queryItem;
	[queryItem release];	
}

-(void)SendTrafficTextRequest{	
	if( self.bRequestSent == false)
	{
		self.bRequestSent   = true;
		[webView loadHTMLString:NSLocalizedString(@"iBabyNotes",@"") baseURL:[NSURL URLWithString:WEBSITE_URL]];
		ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:WEBSITE_URL]];		
		[request setDelegate:self];
		[request startAsynchronous];
	}
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
	self.bRequestSent   = false;
	// Use when fetching text data
	NSString *responseString = [request responseString];
	// Use when fetching binary data
	NSData *responseData = [request responseData];
	NSLog(@"Receive HTML content: %@",responseString);
	
	self.trafficResult = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	NSLog(@"Convert to HTML content: %@",self.trafficResult);
	[webView loadHTMLString:self.trafficResult baseURL:[NSURL URLWithString:WEBSITE_URL]];
	[self SetBarItemAsButton];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
	NSError *error = [request error];
	NSLog(@"Request failed! %@",error.localizedDescription);
	
	self.bRequestSent  = false;
	self.trafficResult = NSLocalizedString(@"No Network Connection", @"");
	[webView loadHTMLString:self.trafficResult baseURL:[NSURL URLWithString:WEBSITE_URL]];
	
	[self SetBarItemAsButton];
}

- (void)dealloc {
    [super dealloc];
}
@end
